package appx.craft.asd.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import appx.craft.asd.R;
import appx.craft.asd.bean.ListItems;
import appx.craft.asd.uc.Adapter_Profile_listview;
import appx.craft.asd.uc.ApplicationTopBar;

// TODO: Auto-generated Javadoc
/**
 * The Class ScrollableActivity.
 */
public class ScrollableActivity extends Activity implements OnItemClickListener,OnFocusChangeListener{
	
	/** The tag. */
	public final String TAG = ScrollableActivity.class.getCanonicalName();
	
	/** The listview_profile. */
	private ListView listview_profile;
	
	/** The m profile adapter. */
	private Adapter_Profile_listview mProfileAdapter;
	
	/** The m top bar. */
	private ApplicationTopBar mTopBar;
	
	/** The m pop up text. */
	int mPopUpText = 0;
	
	/** The m stackofviews. */
	ArrayList<ListItems> mStackofviews=new ArrayList<ListItems>();
	
	/** The show dialog display. */
	static int SHOW_DIALOG_DISPLAY = 100;
	
	/** The show dialog status. */
	static int SHOW_DIALOG_STATUS = 200;
	
	/** The m dialog. */
	Dialog mDialog;
	
	/** The item_display name. */
	String item_displayName="";
	
	/** The item_status. */
	String item_status="";
	
	/** The resource id. */
	int resourceID = R.drawable.icon_person;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState != null){
			    // Restore value of members from saved state
			resourceID = savedInstanceState.getInt("IconID");
			item_displayName = savedInstanceState.getString("Display_Name");
			item_status = savedInstanceState.getString("Status");
			mPopUpText = savedInstanceState.getInt("LastItemSelected");
		}
		Log.d(TAG,"[ onCreate ] ");
		setContentView(R.layout.activity_scrollbar_screen);
		mTopBar = (ApplicationTopBar)findViewById(R.id.top_bar_application);
		mTopBar.setTitle(getResources().getString(R.string.activity_scrollable));
		listview_profile = (ListView)findViewById(R.id.listview_profile);
		listview_profile.setOnItemClickListener(this);
		mProfileAdapter = new Adapter_Profile_listview(ScrollableActivity.this);
		listview_profile.setAdapter(mProfileAdapter);
		
		// item image
		ListItems mItemBeans = null;
		mItemBeans = new ListItems();
		mItemBeans.setItem1("");
		mItemBeans.setItem2("");
		mItemBeans.setType(ListItems.TYPE_IMAGE);
		mItemBeans.setResourceID(resourceID);
		mStackofviews.add(mItemBeans);
		
		//item 1 
		mItemBeans = new ListItems();
		mItemBeans.setItem1("Display Name");
		mItemBeans.setItem2(item_displayName);
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		//item 2
		mItemBeans = new ListItems();
		mItemBeans.setItem1("Status");
		mItemBeans.setItem2(item_status);
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		//item 3
		mItemBeans = new ListItems();
		mItemBeans.setItem1("item 2");
		mItemBeans.setItem2("");
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		//item mobile number
		mItemBeans = new ListItems();
		mItemBeans.setItem1("Mobile");
		mItemBeans.setItem2("+659898439843");
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		//Email
		mItemBeans = new ListItems();
		mItemBeans.setItem1("Email");
		mItemBeans.setItem2("test@gmail.com");
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		//Change Password
		mItemBeans = new ListItems();
		mItemBeans.setItem1("item 3");
		mItemBeans.setItem2("item 3");
		mItemBeans.setType(ListItems.TYPE_ONOFF);
		mStackofviews.add(mItemBeans);
		
		//Change Password
		mItemBeans = new ListItems();
		mItemBeans.setItem1("Change Password");
		mItemBeans.setItem2("");
		mItemBeans.setType(ListItems.TYPE_TEXT);
		mStackofviews.add(mItemBeans);
		
		mProfileAdapter.addItems(mStackofviews);
		
	}
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	protected void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		ListItems mItemsSaved;
		mItemsSaved = (ListItems)mProfileAdapter.getItem(0);
		savedInstanceState.putInt("IconID",mItemsSaved.getResourceID());
		mItemsSaved = (ListItems)mProfileAdapter.getItem(1);
		savedInstanceState.putString("Display_Name",mItemsSaved.getItem2());
		mItemsSaved = (ListItems)mProfileAdapter.getItem(2);
		savedInstanceState.putString("Status",mItemsSaved.getItem2());
		savedInstanceState.putInt("LastItemSelected",mPopUpText);
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"[ onStart ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG,"[ onStop ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG,"[ onPause ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG,"[ onResume ] ");
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"[ onDestroy ] ");
		if(mDialog != null){
			mDialog.dismiss();
		}
	}
	
	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		Toast.makeText(getApplicationContext(),""+position,Toast.LENGTH_LONG).show();
		if(position == 7){
			startActivityForResult(new Intent(ScrollableActivity.this,ChnagePasswordActivity.class),101);
		}else if(position == 1){
			mPopUpText = position;
			showEditDialog(SHOW_DIALOG_DISPLAY);
			OpenKeyBoard();
		}else if(position == 2){
			mPopUpText = 2;
			showEditDialog(SHOW_DIALOG_STATUS);
			OpenKeyBoard();
		}
	}
	
	/**
	 * Show edit dialog.
	 *
	 * @param id the id
	 */
	public void showEditDialog(int id){
		
		if(id == SHOW_DIALOG_DISPLAY){
			
			mDialog = new Dialog(ScrollableActivity.this);
			mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			mDialog.setContentView(R.layout.custom_dialog);
			final TextView mTextView = (TextView)mDialog.findViewById(R.id.textview_dialog_title);
			mTextView.setText(mStackofviews.get(mPopUpText).getItem1());
			final EditText meditView= (EditText)mDialog.findViewById(R.id.edittext_input_items);
			meditView.setOnFocusChangeListener(this);
			meditView.setText(mStackofviews.get(mPopUpText).getItem2());
			meditView.setOnKeyListener(new OnKeyListener() {
				
				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					
					if(keyCode == KeyEvent.KEYCODE_ENTER){
						  hideKeyBoard(meditView);
						  mStackofviews.remove(mPopUpText);
			            	ListItems mBeanObject = null;
			       			
			            	 //item 1 
			           		mBeanObject = new ListItems();
			           		if(mPopUpText == 1){
			           			mBeanObject.setItem1("Display Name");	
			           		}else if(mPopUpText == 2){
			           			mBeanObject.setItem1("Status");	
			           		}
			           		
			           		mBeanObject.setItem2(meditView.getText().toString());
			           		mBeanObject.setType(ListItems.TYPE_TEXT);
			           		mStackofviews.add(mPopUpText,mBeanObject);
			        		
			           		mProfileAdapter.addItems(mStackofviews);
			           		
						  mDialog.dismiss();
						return true;
					}
					return false;
				}
			});
			Button mOk = (Button)mDialog.findViewById(R.id.btn_scrollable_ok);
			mOk.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
						hideKeyBoard(meditView);
						mStackofviews.remove(mPopUpText);
		            	ListItems mBeanObject = null;
		       			
		            	 //item 1 
		           		mBeanObject = new ListItems();
		           		if(mPopUpText == 1){
		           			mBeanObject.setItem1("Display Name");	
		           		}else if(mPopUpText == 2){
		           			mBeanObject.setItem1("Status");	
		           		}
		           		
		           		mBeanObject.setItem2(meditView.getText().toString());
		           		mBeanObject.setType(ListItems.TYPE_TEXT);
		           		mStackofviews.add(mPopUpText,mBeanObject);
		        		
		           		mProfileAdapter.addItems(mStackofviews);
		           		
		           		mDialog.dismiss();
				}
			});
			Button mCancel  = (Button)mDialog.findViewById(R.id.btn_scrollable_cancel);
			mCancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					  hideKeyBoard(meditView);
					  mDialog.dismiss();
				}
			});
			mDialog.show();
			
		}else if(id == SHOW_DIALOG_STATUS ){
			

			mDialog = new Dialog(ScrollableActivity.this);
			mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			mDialog.setContentView(R.layout.custom_dialog);
			final TextView mTextView = (TextView)mDialog.findViewById(R.id.textview_dialog_title);
			mTextView.setText(mStackofviews.get(mPopUpText).getItem1());
			final EditText meditView= (EditText)mDialog.findViewById(R.id.edittext_input_items);
			meditView.setOnFocusChangeListener(this);
			meditView.setText(mStackofviews.get(mPopUpText).getItem2());
			meditView.setOnKeyListener(new OnKeyListener() {
				
				@Override
				public boolean onKey(View v, int keyCode, KeyEvent event) {
					
					if(keyCode == KeyEvent.KEYCODE_ENTER){
						  hideKeyBoard(meditView);
						  mStackofviews.remove(mPopUpText);
			            	ListItems mBeanObject = null;
			       			
			            	 //item 1 
			           		mBeanObject = new ListItems();
			           		if(mPopUpText == 1){
			           			mBeanObject.setItem1("Display Name");	
			           		}else if(mPopUpText == 2){
			           			mBeanObject.setItem1("Status");	
			           		}
			           		
			           		mBeanObject.setItem2(meditView.getText().toString());
			           		mBeanObject.setType(ListItems.TYPE_TEXT);
			           		mStackofviews.add(mPopUpText,mBeanObject);
			        		
			           		mProfileAdapter.addItems(mStackofviews);
						  mDialog.dismiss();
						return true;
					}
					return false;
				}
			});
			Button mOk = (Button)mDialog.findViewById(R.id.btn_scrollable_ok);
			mOk.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
						hideKeyBoard(meditView);
						mStackofviews.remove(mPopUpText);
		            	ListItems mBeanObject = null;
		       			
		            	 //item 1 
		           		mBeanObject = new ListItems();
		           		if(mPopUpText == 1){
		           			mBeanObject.setItem1("Display Name");	
		           		}else if(mPopUpText == 2){
		           			mBeanObject.setItem1("Status");	
		           		}
		           		
		           		mBeanObject.setItem2(meditView.getText().toString());
		           		mBeanObject.setType(ListItems.TYPE_TEXT);
		           		mStackofviews.add(mPopUpText,mBeanObject);
		        		
		           		mProfileAdapter.addItems(mStackofviews);
		           		
		           		mDialog.dismiss();
				}
			});
			Button mCancel  = (Button)mDialog.findViewById(R.id.btn_scrollable_cancel);
			mCancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					  hideKeyBoard(meditView);
					  mDialog.dismiss();
				}
			});
			mDialog.show();
		}
		
	
	}
	

	/**
	 * Hide key board.
	 *
	 * @param editTextTemp the edit text temp
	 */
	public void hideKeyBoard(EditText editTextTemp){
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if(editTextTemp != null){
			imm.hideSoftInputFromWindow(editTextTemp.getWindowToken(), 0);
		}
	}
	
	/**
	 * Open key board.
	 */
	public void OpenKeyBoard(){
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}
		
	public void onToggleClicked(View view) {
	    // Is the toggle on?
	    boolean on = ((ToggleButton) view).isChecked();
	    
	    if (on) {
	        // Enable vibrate
	    	Toast.makeText(getApplicationContext(), "on", Toast.LENGTH_SHORT).show();
	    } else {
	        // Disable vibrate
	    	Toast.makeText(getApplicationContext(), "off", Toast.LENGTH_SHORT).show();
	    }
	}


	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		
		View mView = v;
		
		if(mView instanceof EditText){
			hideKeyBoard((EditText)mView);
		}
		
	}
	
	

}
